package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.service.IEmployeeService;
import com.cn.jdia.tas.service.impl.IAttrecordServiceImpl;
import com.google.gson.Gson;

@Controller
@RequestMapping("/control")
public class TodayController {
	
	@Resource
	private IEmployeeService employeeService=null;
	
	@Resource
	private IAttrecordServiceImpl attService=null;
	
	@RequestMapping("/todayoverview")
	public String checkLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		Map<String,String> map=new HashMap<>();
		
//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		employeeService= (IEmployeeService)w_context.getBean("employeeService");
		
		employeeService= (IEmployeeService)SpringContextUtils.getContext().getBean("employeeService");
		
		int fullcount = employeeService.getEmployeeCount();
		System.out.println(fullcount);
		
		String dt=new DateTime().toString("YYYY-MM-dd");
		System.out.println(dt);
		
//		attService= (IAttrecordServiceImpl)w_context.getBean("attrecordService");
		attService= (IAttrecordServiceImpl)SpringContextUtils.getContext().getBean("attrecordService");
		
		int checkedcount = attService.getCheckedCountByDay(dt);
		System.out.println(checkedcount);
		
		int latecount=attService.getLateCount(dt);
		
		map.put("all", fullcount+"");
		map.put("checked", checkedcount+"");
		map.put("late", latecount+"");		
		
		String ret = gson.toJson(map);
		out.write(ret);
		out.flush();
		out.close();
		
		return null;
	}
	
}
